<template>
  <div class="add_table_css">
    <el-button plain @click="addEducation" class="btn_css_newadd">新增继续教育信息</el-button>
    <span style="color:red;margin-left:20px">包括参加专业学习、培训、国内外进修等</span>
    <!-- 新增继续教育信息的弹框 -->
    <el-dialog title="新增继续教育信息" :visible.sync="dialogFormVisible" width="40%" center>
      <el-form
        label-width="150px"
        :model="teachTable"
        :rules="rules"
        ref="teachTable"
        class="demo-ruleForm add_education_css"
      >
        <el-form-item label="继续教育年度" prop="studyYear">
          <el-select v-model="teachTable.studyYear">
            <el-option label="2020" value="2020"></el-option>
            <el-option label="2021" value="2021"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="专业或主要内容" prop="major">
          <el-input v-model="teachTable.major"></el-input>
        </el-form-item>
        <el-form-item label="学习地点" prop="address">
          <el-input v-model="teachTable.address"></el-input>
        </el-form-item>
        <el-form-item label="学时" prop="period">
          <el-input v-model="teachTable.period"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer add_footer_btn_css">
        <el-button plain @click="addOk('teachTable')" class="btn_css_bord">保存</el-button>
        <el-button plain @click="dialogFormVisible = false" class="btn_css_bord">关闭</el-button>
      </div>
    </el-dialog>
    <!-- 表格 -->
    <el-container>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" label="序号" width="50"></el-table-column>
        <el-table-column prop="studyYear" label="继续教育年度" width="120"></el-table-column>
        <el-table-column prop="major" label="专业或主要内容" width="250"></el-table-column>
        <el-table-column prop="address" label="学习地点" width="200"></el-table-column>
        <el-table-column prop="period" label="学时" width="100"></el-table-column>
        <el-table-column label="操作" width="220">
          <template slot-scope="scope">
            <div class="btnDiv1_css">
              <el-button
                @click.native.prevent="update(scope.$index, tableData)"
                plain
                size="small"
                class="btn_css_er"
              >修改</el-button>
              <el-button
                @click.native.prevent="del(scope.$index, tableData)"
                plain
                size="small"
                class="btn_css_er"
              >删除</el-button>
            </div>
            <div>
              <el-button
                @click.native.prevent="del(scope.$index, tableData)"
                plain
                size="small"
                class="btn_css_si"
              >上传附件</el-button>
              <el-button
                @click.native.prevent="del(scope.$index, tableData)"
                plain
                size="small"
                class="btn_css_si"
              >查看附件</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-container>
    <!-- 修改继续教育信息的弹框 -->
    <el-dialog title="修改继续教育信息" :visible.sync="dialogFormVisible2" width="40%" center>
      <el-form
        label-width="150px"
        :model="teachTable2"
        :rules="rules"
        ref="teachTable2"
        class="demo-ruleForm add_education_css"
      >
        <el-form-item label="继续教育年度" prop="studyYear">
          <el-select v-model="teachTable2.studyYear">
            <el-option label="2020" value="2020"></el-option>
            <el-option label="2021" value="2021"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="专业或主要内容" prop="major">
          <el-input v-model="teachTable2.major"></el-input>
        </el-form-item>
        <el-form-item label="学习地点" prop="address">
          <el-input v-model="teachTable2.address"></el-input>
        </el-form-item>
        <el-form-item label="学时" prop="period">
          <el-input v-model="teachTable2.period"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer add_footer_btn_css">
        <el-button plain @click="updateOk('teachTable2')" class="btn_css_bord">保存</el-button>
        <el-button plain @click="dialogFormVisible2 = false" class="btn_css_bord">关闭</el-button>
      </div>
    </el-dialog>
    <!-- 分页 -->
    <div class="block page_css">
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :page-size="limit"
        @current-change="change"
        @prev-click="prev"
        @next-click="next"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      page: 1,
      limit: 5,
      total: 0,
      staffId: 7,
      // id:8,

      tableData: [],
      dialogFormVisible: false,
      dialogFormVisible2: false,
      teachTable: {
        studyYear: "",
        major: "",
        address: "",
        period: ""
      },
      teachTable2: {
        studyYear: "",
        major: "",
        address: "",
        period: ""
      },
      rules: {
        studyYear: [
          { required: true, message: "请输入毕业院校", trigger: "blur" }
        ],
        major: [{ required: true, message: "请输入毕业院校", trigger: "blur" }],
        address: [
          { required: true, message: "请输入毕业院校", trigger: "blur" }
        ],
        period: [{ required: true, message: "请输入毕业院校", trigger: "blur" }]
      }
    };
  },
  methods: {
    /* 页面渲染 */
    getCon() {
      this.$axios({
        url: "/teach/list",
        method: "post",
        data: {
          page: this.page,
          limit: this.limit,
          staffId: this.staffId
        }
      }).then(res => {
        // console.log(res.data);
        this.tableData = res.data.data;
        this.total = res.data.count;
      });
    },
    /* 新增 */
    addEducation() {
      this.dialogFormVisible = true;
    },
    addOk(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$axios({
            url: "/teach/add",
            method: "post",
            data: {
              // id: this.id,
              staffId: this.staffId,
              studyYear: this.teachTable.studyYear,
              major: this.teachTable.major,
              address: this.teachTable.address,
              period: this.teachTable.period
            }
          }).then(res => {
            console.log(res);
            this.getCon();
            this.dialogFormVisible = false;
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    /* 修改渲染 */
    update(index, data) {
      console.log(data[index]);
      this.dialogFormVisible2 = true;
      this.teachTable2 = { ...data[index]};
    },
    updateOk(formName) {
      // console.log(this.teachTable2.id);
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$axios({
            url: "/teach/update",
            method: "post",
            data: {
              id:this.teachTable2.id,
              studyYear: this.teachTable2.studyYear,
              major: this.teachTable2.major,
              address: this.teachTable2.address,
              period: this.teachTable2.period
            }
          }).then(res => {
            // console.log(res);
            this.getCon();
            this.dialogFormVisible2 = false;
            this.$message.success('修改成功!');
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    /* 删除渲染 */
    del(index, data) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$axios({
          url:'/teach/deleteTeachById',
          method:'post',
          data:{
            id:data[index].id,
          }
        }).then(res => {
          console.log(res);
          this.getCon();
          this.$message.success('删除成功！');
          
        })
      })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },

    /* 分页渲染 */
    change(page) {
      this.page = page;
      this.getCon();
    },
    prev() {
      this.page--;
      this.getCon();
    },
    next() {
      this.page++;
      this.getCon();
    },

    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          //   alert("submit!");
          this.dialogFormVisible = false;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  },

  // mounted(){
  //   sessionStorage.setItem('activeIndex', 1)
  // }
  mounted() {
    this.getCon();
    sessionStorage.setItem("fName", "Process6");
  }
};
</script>
<style scoped lang="less">
.btnDiv1_css {
  margin-bottom: 6px;
  .el-button:first-child {
    margin-right: 20px;
  }
}
.page_css {
  margin: 10px;
  text-align: right;
}
.el-table {
  font-size: 13px;
}
.btn_css_newadd {
  background-color: #f7f7f7;
  border: 1px solid #eee;
  text-align: center;
}
.btn_css_er {
  background-color: #f7f7f7;
  border: 1px solid #eee;
  width: 60px;
  height: 30px;
  text-align: center;
}
.btn_css_si {
  background-color: #f7f7f7;
  border: 1px solid #eee;
  width: 80px;
  height: 30px;
  text-align: center;
}
.btn_css_bord {
  background-color: #f7f7f7;
  border: 1px solid #eee;
  width: 80px;
  height: 40px;
  font-size: 14px;
  text-align: center;
}
.add_education_css {
  width: 370px;
  margin-left: 40px;
}
.add_footer_btn_css {
  margin: 0 auto;
}
.add_table_css {
  border: 1px solid #eee;
  // height: 600px;
  padding: 20px;
}
</style>